﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using ForumLibrary;
using ForumLibrary.Data;
using System.Drawing;

public partial class Notifications : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["username"] != null)
        {
            HtmlGenericControl forum = (HtmlGenericControl)Page.Master.FindControl("Forum");
            forum.Attributes.Remove("class");

            HtmlGenericControl notifications = (HtmlGenericControl)Page.Master.FindControl("Notifications");
            notifications.Attributes.Add("class", "active");

            HtmlGenericControl account = (HtmlGenericControl)Page.Master.FindControl("Account");
            account.Attributes.Remove("class");

            Table1.EnableViewState = true;
            insertLineToNots("Notification Details", "Time", 0);
            Table1.Rows[0].Font.Bold = true;
            Table1.Rows[0].BorderWidth = 4;

            this.refillNots();
        }
    }

    private void insertLineToNots(string text1, string text2, int where)
    {
        TableRow r = new TableRow();
        TableCell c = new TableCell();
        c.Controls.Add(new LiteralControl(text1));
        r.Cells.Add(c);
        c = new TableCell();
        c.Controls.Add(new LiteralControl(text2));
        r.Cells.Add(c);
        Table1.Rows.AddAt(where, r);
    }

    private void refillNots()
    {
        // Generate new rows and cells. mark old notifications (if any) as read.
        List<Notification> nots = new ForumWebManager().getUserNotifications(Session["username"].ToString());
        for (int j = 0; j < nots.Count; j++)
        {
            insertLineToNots(nots.ElementAt(j).NotificationMessage,nots.ElementAt(j).timeStamp,1);//TODO
            if(nots.ElementAt(j).delivered)
                Table1.Rows[1].Font.Bold = false;//1 is the index in table of new element
            else
                Table1.Rows[1].Font.Bold = true;
        }
        if (nots.Count == 0)
        {//no notifications => no table
            Table1.Rows.RemoveAt(0);
            Label1.Visible = true;
        }
    }
}